package net.cl.db.handler;

import java.sql.Types;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class JavaTypeHandler {

	private static final Map<Integer, String> JAVA_TYPE_MAP = getJavaTypeMap();

	private static Map<Integer, String> getJavaTypeMap() {
		Map<Integer, String> map = new HashMap<Integer, String>();
		map.put(Types.ARRAY, Object.class.getSimpleName());
		map.put(Types.BIGINT, Long.class.getSimpleName());
		map.put(Types.BINARY, "byte[]");
		map.put(Types.BIT, Boolean.class.getSimpleName());
		map.put(Types.BLOB, "byte[]");
		map.put(Types.BOOLEAN, Boolean.class.getSimpleName());
		map.put(Types.CHAR, String.class.getSimpleName());
		map.put(Types.CLOB, String.class.getSimpleName());
		map.put(Types.DATALINK, Object.class.getSimpleName());
		map.put(Types.DATE, Date.class.getSimpleName());
		map.put(Types.DECIMAL, Double.class.getSimpleName());
		map.put(Types.DISTINCT, Object.class.getSimpleName());
		map.put(Types.DOUBLE, Double.class.getSimpleName());
		map.put(Types.FLOAT, Float.class.getSimpleName());
		map.put(Types.INTEGER, Integer.class.getSimpleName());
		map.put(Types.JAVA_OBJECT, Object.class.getSimpleName());
		map.put(Types.LONGVARBINARY, "byte[]");
		map.put(Types.LONGVARCHAR, String.class.getSimpleName());
		map.put(Types.NCHAR, String.class.getSimpleName());
		map.put(Types.NCLOB, String.class.getSimpleName());
		map.put(Types.NULL, Object.class.getSimpleName());
		map.put(Types.NUMERIC, Double.class.getSimpleName());
		map.put(Types.NVARCHAR, String.class.getSimpleName());
		map.put(Types.OTHER, Object.class.getSimpleName());
		map.put(Types.REAL, Float.class.getSimpleName());
		map.put(Types.REF, Object.class.getSimpleName());
		map.put(Types.SMALLINT, Short.class.getSimpleName());
		map.put(Types.STRUCT, Object.class.getSimpleName());
		map.put(Types.TIME, Date.class.getSimpleName());
		map.put(Types.TIMESTAMP, Date.class.getSimpleName());
		map.put(Types.TINYINT, Byte.class.getSimpleName());
		map.put(Types.VARBINARY, "byte[]");
		map.put(Types.VARCHAR, String.class.getSimpleName());
		return map;
	}

	public static String getJavaType(Integer jdbcType) {
		return getJavaType(jdbcType, 0);
	}

	public static String getJavaType(Integer jdbcType, int scale) {
		String javaType = JAVA_TYPE_MAP.get(jdbcType);
		if (scale == 0 && "Double".equals(javaType))
			return "Long";
		return javaType;
	}

}
